﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;

using Warehouse.Silverlight.Core.Infrastructure;
using Warehouse.Silverlight.Modules.Main.Models;
using Warehouse.Silverlight.Modules.Main.ViewModels;

namespace Warehouse.Silverlight.Modules.Main.Views
{
    [Export]
    [ViewSortHint("01")]
    public partial class NavTreeView : UserControl
    {
        public NavTreeView()
        {
            InitializeComponent();
        }

        [Import]
        public NavTreeViewModel ViewModel
        {
            get { return this.DataContext as NavTreeViewModel; }
            set { this.DataContext = value; }
        }

        // UI Events
        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var item = (NavTreeItem)e.NewValue;

            this.Dispatcher.BeginInvoke(() =>
            {
                ViewModel.SelectedItem = item;
            });
        }
    }
}
